// pages/my/myQRcode/index.js
let QRCode = require('../../../utils/QRcode')
Page({
  data: {
    orderNo: "https://www.baidu.com/", //二维码内容
    codeImgPath: "", //图片路径
    userAvatar: "",
    userName: "",
    userCode: "",
    pageBg: "", //页面背景
    qrCardBg: "", //二维码背景图
    imgDraw: null, //分享图对象
    userCardImg: "", //用户卡片海报
    isReady: [false, false]
  },
  // 生成海报
  drawPic() {
    // console.log('生成用户卡片');
    let imgDraw = {
      "width": "654rpx",
      "height": "1006rpx",
      "position": "relative",
      "background": "#1A1A1A",
      "views": [
        // 背景
        {
          "type": "image",
          "url": this.data.qrCardBg,
          "mode": "widthFix",
          "css": {
            "width": "654rpx",
            "height": "1006rpx",
          }
        },
        // 头像
        {
          "type": "image",
          "mode": "widthFix",
          "url": this.data.userAvatar,
          // "mode": "auto",
          "css": {
            "width": "160rpx",
            "height": "160rpx",
            "left": "246rpx",
            "top": "82rpx",
            "borderRadius": "50%"
          }
        },
        // 用户名
        {
          "type": "text",
          "text": this.data.userName,
          "css": {
            "top": "274rpx",
            "left": "327rpx",
            "fontSize": "32rpx",
            "color": "#1A1A1A",
            "align": "center"
          },
        },
        // 草图里号
        {
          "type": "text",
          "text": "草图里号 " + this.data.userCode,
          "css": {
            "top": "334rpx",
            "left": "327rpx",
            "fontSize": "24rpx",
            "color": "rgba(0, 0, 0, 0.6)",
            "align": "center"
          },
        },
        // 二维码
        {
          "type": "image",
          "mode": "widthFix",
          "url": this.data.codeImgPath,
          "css": {
            "width": "276rpx",
            "height": "276rpx",
            "left": "188rpx",
            "top": "424rpx"
          }
        }
      ]
    }
    this.setData({
      imgDraw
    })
  },
  // 绘图成功
  onImgOK(e) {
    // console.log('绘图成功', e);
    if (e.detail.path) {
      this.setData({
        userCardImg: e.detail.path
      })
    }
  },
  // 绘图失败
  onImgErr(e) {
    console.log('绘图失败', e);
  },
  // 创建二维码
  createQRCode() {
    let _this = this
    new QRCode('myQrcode', {
      // text: `/pages/my/othersPage/index?id=${wx.getStorageSync('userInfo').UserId}`,
      text: `ctl://openuser:${wx.getStorageSync('userInfo').UserCode}`,
      width: 200, //canvas 画布的宽
      height: 200, //canvas 画布的高
      padding: 0, // 生成二维码四周自动留边宽度，不传入默认为0
      correctLevel: QRCode.CorrectLevel.L, // 二维码可辨识度
      colorDark: "#000000", //分别为两种交替颜色
      colorLight: "white",
      callback: (res) => {
        // console.log('生成二维码', res);
        _this.setData({
          codeImgPath: res.path
        })
        _this.data.isReady[1] = true
        if (_this.data.isReady.every(item => item)) {
          _this.drawPic()
        }
        // wx.downloadFile({
        //   url: res.path,
        //   success: res => {
        //     _this.setData({
        //       codeImgPath: res.tempFilePath
        //     })
        //   }
        // })
      }
    })
  },
  // 扫一扫
  scan() {
    wx.scanCode({
      success(res) {
        console.log('扫码结果', res)
        if (res.result.includes('openuser')) {
          let code = res.result.split('openuser:')[1]
          wx.navigateTo({
            url: `/pages/my/othersPage/index?code=${code}`,
          })
        }
      }
    })
  },
  // 保存图片到相册
  saveImage() {
    // console.log('保存图片到相册', this.data.userCardImg);
    wx.saveImageToPhotosAlbum({
      filePath: this.data.userCardImg,
      success(res) {
        // console.log('保存成功', res);
        wx.showToast({
          title: '保存成功',
          icon: "success"
        })
      },
      fail(err) {
        if (err.errMsg.includes('fail auth deny')) {
          wx.getSetting({
            success(res) {
              if (!res.authSetting['scope.writePhotosAlbum']) {
                wx.showModal({
                  title: '提示',
                  content: '需要您授权保存相册',
                  success() {
                    wx.openSetting({
                      success(res) {
                        console.log('设置成功', res);
                      }
                    })
                  }
                })
              }
            }
          })
        }

      }
    })
  },
  // 复制草图里号
  copyCode() {
    wx.setClipboardData({
      data: `${this.data.userCode}`,
      success(res) {
        wx.showToast({
          title: '复制成功',
          icon: 'none'
        });
      }
    });
  },
  onLoad(options) {
    this.createQRCode()
    this.setData({
      userAvatar: wx.getStorageSync('userInfo')?.Avatar,
      userName: wx.getStorageSync('userInfo')?.UserName,
      userCode: wx.getStorageSync('userInfo')?.UserCode
    })
    wx.downloadFile({
      url: "https://staticimg.aiiiin.com/wxapp/my_qr_bg.png",
      success: (res) => {
        this.setData({
          pageBg: res.tempFilePath
        })
        // console.log('背景', this.data.pageBg);
      }
    })
    wx.downloadFile({
      url: "https://staticimg.aiiiin.com/wxapp/qr_card.png",
      success: (res) => {
        this.setData({
          qrCardBg: res.tempFilePath,
        })
        this.data.isReady[0] = true
        if (this.data.isReady.every(item => item)) {
          this.drawPic()
        }
        // console.log('背景', this.data.qrCardBg);
      }
    })
  }
})